﻿
using GameFramework;
using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace LS.UI
{
    public abstract class MessageBoxUiDocumentForm : UEuiForm
    {
        protected bool m_PauseGame;
        protected string[] m_Buttons;
        protected string m_Title;
        protected string m_Message;

        protected GameFrameworkAction<int> m_OnButtonClickOnceEvent = null;

        public UnityEvent<int> OnButtonClick = new UnityEvent<int>();

        public int Mode
        {
            get
            {
                return m_Buttons?.Length ?? 0;
            }
        }

        public string Title => m_Title;

        public string Message => m_Message;

        public bool PauseGame
        {
            get
            {
                return m_PauseGame;
            }
        }

        public override bool FadeWithoutTimeScale => false;

#if UNITY_2017_3_OR_NEWER
        protected override void OnOpen(object userData)
#else
        protected internal override void OnOpen(object userData)
#endif
        {
            MessageBoxParams messageBoxParams = (MessageBoxParams)userData;
            if(messageBoxParams != null)
            {
                m_PauseGame = messageBoxParams.PauseGame;
                m_Buttons = messageBoxParams.ButtonLabels.ToArray();
                m_Title = messageBoxParams.Title;
                m_Message = messageBoxParams.Message;
                m_OnButtonClickOnceEvent = messageBoxParams.ButtonClickEvent;
            }

            base.OnOpen(userData);

            if (PauseGame)
            {
                LSEntry.Base.PauseGame();
            }
        }

        protected override void OnResume()
        {
            base.OnResume();
            RefreshTitle(m_Title);
            RefreshMessageText(m_Message);
            RefreshButtons(m_Buttons);
        }

        protected override void OnPause()
        {
            base.OnPause();
        }

#if UNITY_2017_3_OR_NEWER
        protected override void OnClose(bool isShutdown, object userData)
#else
        protected internal override void OnClose(bool isShutdown, object userData)
#endif
        {
            if (PauseGame)
            {
                LSEntry.Base.ResumeGame();
            }
            m_Buttons = null;
            m_Title = null;
            m_Message = null;
            m_OnButtonClickOnceEvent = null;

            RefreshTitle(string.Empty);
            RefreshMessageText(string.Empty);
            RefreshButtons(new string[0]);
            
            base.OnClose(isShutdown, userData);
        }
        protected abstract void RefreshTitle(string title);

        protected abstract void RefreshMessageText(string text);

        protected abstract void RefreshButtons(string[] texts);

        protected void OnButtonClickEvent(int index)
        {
            var eventOnce = m_OnButtonClickOnceEvent;
            Close(true);
            OnButtonClick.Invoke(index);
            eventOnce?.Invoke(index);
        }
    }
}
